package com.modificial.springboot.service;

import com.modificial.springboot.job.TestSchedule;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * @author modificial
 */
@Service
public class ScheduleService {
    @Autowired
    private Scheduler scheduler;

    public void testScheduleTask() throws SchedulerException {
        //  定时执行的任务
        JobDetail jobDetail = JobBuilder.newJob(TestSchedule.class)
                .withIdentity("updateMatch", "updateMatch")
                .withDescription("定时比赛Id为")
                .build();
        //cron表达式 表示每隔i秒执行
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(String.format("0/%d * * * * ? ", 1))
                .withMisfireHandlingInstructionDoNothing();
        //创建触发器
        CronTrigger cronTrigger = TriggerBuilder.newTrigger()
                .withIdentity("updateMatch", "updateMatch")
                .withDescription("定时比赛Id为" + UUID.randomUUID().toString())
                .withSchedule(scheduleBuilder)
                .startNow().build();
        //加入任务计划
        scheduler.scheduleJob(jobDetail, cronTrigger);

    }

}